package me.firstapp.service.system.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.MD5Utils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.system.ApiClient;
import me.firstapp.module.system.Operator;
import me.firstapp.repository.system.ApiClientRepository;
import me.firstapp.repository.system.OperatorRepository;
import me.firstapp.service.system.ApiClientService;

@Service
public class ApiClientServiceImpl implements ApiClientService {

	@Autowired
	private ApiClientRepository apiClientRepository;

	@Autowired
	private OperatorRepository operatorRepository;

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Override
//	@Cacheable(value = "apiClient", key = "#apiKey")
	public ApiClient findApiClientByKey(String apiKey) {
		if (StrUtils.isNULL(apiKey)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiKey");
		}
		return apiClientRepository.findApiClientByKey(apiKey);
	}

	@Override
	public ApiClient addApiClient(String email, String description) {
		ApiClient apiClient = new ApiClient();
		apiClient.setDescription(description);
		apiClient.setDisableTime(new Date());
		apiClient.setStatus(0);
		apiClient.setYesCounts(0L);
		apiClient.setApiKey(UUID.randomUUID().toString().replaceAll("-", ""));
		apiClient.setApiSecret(MD5Utils.MD5(UUID.randomUUID().toString()));
		apiClient.setCreateTime(new Date());
		apiClient.setEmail(email);
		apiClientRepository.save(apiClient);

		// 添加admin操作员
		Operator operator = new Operator();
		operator.setApiClientId(apiClient.getId());
		operator.setEmail(apiClient.getEmail());
		operator.setName("admin");
		operator.setPassword(MD5Utils.generate("12345678"));
		operator.setStatus(1);
		operatorRepository.save(operator);
		return apiClient;
	}

	@Override
	public ApiClient updateApiClient(Long id, Long yesCounts, String description, Integer status) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		ApiClient apiClient = (ApiClient) apiClientRepository.getById(id);
		if (apiClient == null) {
			throw new ServiceException(StatusHouse.API_CLIENT_NOT_EXIST);
		}
		if (yesCounts != null) {
			apiClient.setYesCounts(yesCounts);
		}
		if (StrUtils.isNotNULL(description)) {
			apiClient.setDescription(description);
		}
		if (status != null) {
			if (status == 0) {
				apiClient.setDisableTime(new Date());
			}
			if (status == 1) {
				apiClient.setEnableTime(new Date());
				apiClient.setDisableTime(null);
			}
			apiClient.setStatus(status);
		}
		apiClientRepository.update(apiClient);
		if (apiClient.getStatus() == 0) {
			redisTemplate.opsForValue().set(apiClient.getApiKey(), null);
		} else if (apiClient.getStatus() == 1) {
			redisTemplate.opsForValue().set(apiClient.getApiKey(), apiClient);
		}
		return apiClient;
	}

	@Override
	public Page<ApiClient> findApiClientPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		return apiClientRepository.findApiClientPage(pageNo, pageSize, searchParams, sortParam, sortType);
	}

	@Override
	public ApiClient findApiClientById(Long id) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		ApiClient apiClient = (ApiClient) apiClientRepository.getById(id);
		return apiClient;
	}

	@Override
	public List<ApiClient> findAll() {
		return apiClientRepository.findAll();
	}

}
